<?php
require_once ('lib/adodb/adodb.inc.php');

class Databases{

  var $confDir='conf/';
  var $config;
  var $dbHost;
  var $dbUser;
  var $dbPass;
  var $dbName;
  var $dbDriver;
  var $conn;
  private $sqlParsed;
  protected $sqlFile=null;
  var $sqlQueries=array();
  
  public function Databases(){

    $this->getConf();
   //print($this->dbDriver);exit;
    $this->conn= &ADONewConnection($this->dbDriver) ;
    //print($this->dbName);
    $this->conn->Connect($this->dbHost,$this->dbUser,$this->dbPass,$this->dbName);
    
  //  $this->Databases = conn;
  }
  
  function getConf(){
    require_once('Configuration.class.php');
    
    $this->config= new Configuration();
    $this->config->setConfDir($this->confDir);
    $this->config->loadConfFile('config.conf.php');
    
    $this->dbHost = $this->config->getValue('db_host');
    $this->dbUser = $this->config->getValue('db_user');
    $this->dbPass = $this->config->getValue('db_pass');
    $this->dbName = $this->config->getValue('db_name');
    $this->dbDriver = $this->config->getValue('db_driver');
    
    $this->loadSqlFile($this->sqlFile);
  }
  
  function loadSqlFile(){
    //$file=$this->config->confValue['doc_root'].$file;
    if(file_exists($this->sqlFile)){
      require($this->sqlFile);
      $this->sqlQueries=$sql;
   }else{
     die('Required Sql File '.$this->sqlFile.' not found!!!');
    }
  }
  
  
  function open($sql,$params){
    $this->sqlParsed="";
    $this->sqlParsed=vsprintf($sql, $params);
    //print($this->sqlParsed);
    $rs=$this->conn->Execute($this->sqlParsed);

    if($rs=$this->conn->Execute($this->sqlParsed)){
      if($rs->RecordCount()>0){
      $i=0;
        while($res=$rs->FetchRow()){
          for($i=0;$i<$rs->FieldCount();$i++){
            $field=$rs->FetchField($i);
            $fieldName=$field->name;
            $arrTemp[$fieldName]=$res[$fieldName];
          }
          $arrResult[]=$arrTemp;
        }
        
      }
      return $arrResult;
    }else{
      die('Queries Failed');
    }
  }
  
  
  
  function execute($sql,$params){
    $this->sqlParsed="";
    $this->sqlParsed=vsprintf($sql, $params);
    if($rs=$this->conn->Execute($this->sqlParsed)){
      return true;
    }else{
      return false;
    }
  }
  
  

  
  
}
?>
